﻿using Stock_App.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
public class stockszManager : DbContext<stocksz>
{

    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 stockszManager.cs

    public List<StockDataShow> GetListSD(DateTime startTime, DateTime endTime, string stockCode)
    {
        string query = @"SELECT
        sz.amplitude Amplitude,
        sz.latest_price LatestPrice,
        sz.change_percent ChangePercent,
        sz.dynamic_pe_ratio DynamicPeRatio,
        sz.turnover_rate TurnoverRate,
        sz.turnover Turnover,
        sz.pb_ratio PbRatio,
        sz.`code`  StockCode,
        sz.`name` StockName,
        sd.industry Industry,
        tsz.i_disk I_Disk,
        tsz.o_disk O_Disk,
        tsz.t_rate T_Rate,
        tsz.v_ratio V_Ratio,
        tsz.creation_time CreationTime 
    FROM
        stocksz sz
    LEFT JOIN stockdata sd ON sd.stock_code = sz.code
    LEFT JOIN stocktradesz tsz ON tsz.s_code = sz.code AND tsz.creation_time = sz.creation_time 
    ";

       
       

        var result = string.IsNullOrEmpty(stockCode)
    ? Db.SqlQueryable<StockDataShow>(query)
        .Where(s => DateTime.Parse(s.CreationTime) >= startTime && DateTime.Parse(s.CreationTime) <= endTime)
        .OrderBy(c=> c.CreationTime).ToList()
    : Db.SqlQueryable<StockDataShow>(query)
        .Where(s => DateTime.Parse(s.CreationTime) >= startTime && DateTime.Parse(s.CreationTime) <= endTime && s.StockCode == stockCode)
        .OrderBy(c => c.CreationTime).ToList();
      

        return result;
    }


}